import request from '@/router/request'
const baseUrl = "/ncApi/plat/nc"
import { dateFormat } from "@/utils/date"

const paramsCodes = {
  temp: {
    file: "T2M",  // 温度
    attr: ["t2m"],
  },
  sst: {
    file: "TEM",  // 海温
    attr: ["sst"],
  },
  salt: {
    file: "SAL",  // 盐度
    attr: ["sst"],
  },
  msl: {
    file: "msl",  // 气压
    attr: ["msl"],
  },
  shww: {
    file: "H03",  // 海浪
    attr: ["swh"],
  },
  flow: {
    file: "CVUV",  // 流
    attr: ["p140208"],
  },
  wind: {
    file: "UV",  // 风
    attr: ["u10", "v10"],
  },
}

const firstTime = "2023-10-20 00:00:00"
// 处理时间，第一天为2023-10-20
const dealTime = (time) => {
  // 先算传进来的时间跟今天差了几天
  const day = (+new Date(dateFormat(time, "yyyy-MM-dd")) - +new Date(dateFormat(new Date(), "yyyy-MM-dd"))) / (24 * 60 * 60 * 1000)
  // 先计算出来年月日
  const ymd = dateFormat(new Date(day * 24 * 60 * 60 * 1000 + +new Date(firstTime)), "yyyy-MM-dd")
  return `${ymd} ${dateFormat(new Date(time), "hh:mm:ss")}`;
}

// 单点数据
export function getWeatherByPoint(params) {
  let param = { ...params }
  param.type = paramsCodes[params.type].file
  param.time = dealTime(param.time)
  return request({
    url: baseUrl + "/point",
    method: 'get',
    params: param
  })
}

// 综合气象请求接口
export function getWeatherByType(params) {
  const param = { ...params }
  param.type = paramsCodes[param.type].file
  param.time = dealTime(param.time)
  return request({
    url: baseUrl + "/metRender",
    method: 'get',
    params: param
  })
}

// 单点气象的所有预报数据
export function getPointListByType(params) {
  let param = { ...params }
  param.type = paramsCodes[params.type].file
  return request({
    url: baseUrl + "/list/point",
    method: 'get',
    params: param
  })
}

// 等值线
export function getFeatureByType(params) {
  let param = { ...params }
  param.type = paramsCodes[params.type].file
  param.time = dealTime(param.time)
  return request({
    url: baseUrl + "/test",
    method: 'get',
    params: param
  })
}

// 配置项
export function getWeatherConfig() {
  return request({
    url: baseUrl + "/config",
    method: 'get',
  })
}

export function setWeatherConfig(data) {
  return request({
    url: baseUrl + "/config",
    method: 'post',
    data
  })
}

// 等值线
export function getWeatherLines(params) {
  let param = { ...params }
  param.type = paramsCodes[params.type].file
  param.time = dealTime(param.time)
  return request({
    url: baseUrl + "/contour/lines",
    method: 'get',
    params: param
  })
}

// 获取文件的时间
export function getFileTime(params) {
  return request({
    url: baseUrl + "/fileTime",
    method: 'get',
    params
  })
}